<template>
  <el-dialog
    :title="!dataForm.tbShopId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="封面图片" prop="tbCoverPicture">
      <el-input v-model="dataForm.tbCoverPicture" placeholder="封面图片"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="tbHeadline">
      <el-input v-model="dataForm.tbHeadline" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="标签" prop="tbLabel">
      <el-input v-model="dataForm.tbLabel" placeholder="标签"></el-input>
    </el-form-item>
    <el-form-item label="地区" prop="tbRegion">
      <el-input v-model="dataForm.tbRegion" placeholder="地区"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="tbArea">
      <el-input v-model="dataForm.tbArea" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="总价" prop="tbTotal">
      <el-input v-model="dataForm.tbTotal" placeholder="总价"></el-input>
    </el-form-item>
    <el-form-item label="装修" prop="tbFitment">
      <el-input v-model="dataForm.tbFitment" placeholder="装修"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="tbTurnovertime">
      <el-input v-model="dataForm.tbTurnovertime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="置顶" prop="tbStick">
      <el-input v-model="dataForm.tbStick" placeholder="置顶"></el-input>
    </el-form-item>
    <el-form-item label="月租" prop="tbMonthlyrent">
      <el-input v-model="dataForm.tbMonthlyrent" placeholder="月租"></el-input>
    </el-form-item>
    <el-form-item label="地区id " prop="tbAreaId">
      <el-input v-model="dataForm.tbAreaId" placeholder="地区id "></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          tbShopId: 0,
          tbCoverPicture: '',
          tbHeadline: '',
          tbLabel: '',
          tbRegion: '',
          tbArea: '',
          tbTotal: '',
          tbFitment: '',
          tbTurnovertime: '',
          tbStick: '',
          tbMonthlyrent: '',
          tbAreaId: ''
        },
        dataRule: {
          tbCoverPicture: [
            { required: true, message: '封面图片不能为空', trigger: 'blur' }
          ],
          tbHeadline: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          tbLabel: [
            { required: true, message: '标签不能为空', trigger: 'blur' }
          ],
          tbRegion: [
            { required: true, message: '地区不能为空', trigger: 'blur' }
          ],
          tbArea: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          tbTotal: [
            { required: true, message: '总价不能为空', trigger: 'blur' }
          ],
          tbFitment: [
            { required: true, message: '装修不能为空', trigger: 'blur' }
          ],
          tbTurnovertime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          tbStick: [
            { required: true, message: '置顶不能为空', trigger: 'blur' }
          ],
          tbMonthlyrent: [
            { required: true, message: '月租不能为空', trigger: 'blur' }
          ],
          tbAreaId: [
            { required: true, message: '地区id 不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.tbShopId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.tbShopId) {
            this.$http({
              url: this.$http.adornUrl(`/twohouse/shop/info/${this.dataForm.tbShopId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tbCoverPicture = data.shop.tbCoverPicture
                this.dataForm.tbHeadline = data.shop.tbHeadline
                this.dataForm.tbLabel = data.shop.tbLabel
                this.dataForm.tbRegion = data.shop.tbRegion
                this.dataForm.tbArea = data.shop.tbArea
                this.dataForm.tbTotal = data.shop.tbTotal
                this.dataForm.tbFitment = data.shop.tbFitment
                this.dataForm.tbTurnovertime = data.shop.tbTurnovertime
                this.dataForm.tbStick = data.shop.tbStick
                this.dataForm.tbMonthlyrent = data.shop.tbMonthlyrent
                this.dataForm.tbAreaId = data.shop.tbAreaId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/twohouse/shop/${!this.dataForm.tbShopId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'tbShopId': this.dataForm.tbShopId || undefined,
                'tbCoverPicture': this.dataForm.tbCoverPicture,
                'tbHeadline': this.dataForm.tbHeadline,
                'tbLabel': this.dataForm.tbLabel,
                'tbRegion': this.dataForm.tbRegion,
                'tbArea': this.dataForm.tbArea,
                'tbTotal': this.dataForm.tbTotal,
                'tbFitment': this.dataForm.tbFitment,
                'tbTurnovertime': this.dataForm.tbTurnovertime,
                'tbStick': this.dataForm.tbStick,
                'tbMonthlyrent': this.dataForm.tbMonthlyrent,
                'tbAreaId': this.dataForm.tbAreaId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
